<?php

namespace Frontend\Modules\Home\Controllers;
use Common\Models\Videos\Video;
use Common\Models\Videos\HomeCategory;

class IndexController extends \FrontendController
{
	public function indexAction()
	{
		try
		{
			$this->assets
            ->addCss('vendor/frontend/css/home/home.css')
            ->addJs('vendor/frontend/js/home/movies_angular.js') 
            ->addJs('vendor/frontend/js/home/movies.js');

            $homeCategories = $this->modelsCache->get($this->constant->keyCache['homeCategory']['category']);
            if ($homeCategories === null) {
                $homeCategories = HomeCategory::find(array(
                    'group' => 'id'
                ));
                $this->modelsCache->save($this->constant->keyCache['homeCategory']['category'] , $homeCategories);
            }

            if($homeCategories)
            {
                $blockVideoHomes = array();
                foreach($homeCategories as $category)
                {
                    $cacheKeyVideoHome = $this->constant->keyCache['homeCategory']['blockVideoCategory'] . '-' . $category->getId();
                    $blockHomeCategory = $this->viewCache->get($cacheKeyVideoHome);
                    if($blockHomeCategory == null)
                    {
                        $videoHomes = $this->modelsManager->createBuilder()
                            ->from(array('homeVideo' => '\Common\Models\Videos\HomeVideo'))
                            //->groupBy("homeVideo.position")
                            ->limit(500)
                            ->where('homeVideo.home_category_id = :home_category_id: AND homeVideo.status = :status:' , array('home_category_id' => $category->getId() , 'status' => 'A'))
                            ->orderBy('homeVideo.position')
                            ->getQuery()
                            ->execute();
                        $blockHomeCategory = $this->view->getRender('home' , 'index/block_video_home' , array('videoHomes' => $videoHomes , 'category' => $category));
                        $this->viewCache->save($cacheKeyVideoHome , $blockHomeCategory);
                    }
                    $blockVideoHomes[] = $blockHomeCategory;
                }
            }

            $this->view->blockVideoHomes = $blockVideoHomes;
            $this->view->render("home" , "index/index");
		}
		catch(\Exception $e)
		{
			$this->flash->error($e->getMessage());
		}
	}

    public function notFoundAction()
    {
        die(print '123');
    }
}